﻿//生成[MaterialGoods]Service代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.IRepository;
using FineAdmin.Common;

namespace FineAdmin.Service
{
    public class MaterialGoodsService : BaseService<MaterialGoodsModel>, IMaterialGoodsService
    {
        public IMaterialGoodsRepository MaterialGoodsRepository { get; set; }
        public IEnumerable<MaterialGoodsModel> GetMaterialGoodsList()
        {
            return MaterialGoodsRepository.GetMaterialGoodsList();
        }


        public dynamic GetListByFilter(MaterialGoodsModel filter, PageInfo pageInfo)
        {
            string _where = " where 1=1";
            if (filter.S_Price.HasValue)
            {

                _where += string.Format(" and CouponAfterPrice >= {0}", filter.S_Price.Value);
            }
            if (filter.B_Price.HasValue)
            {
                _where += string.Format(" and CouponAfterPrice <= {0}", filter.B_Price.Value);

            }
            if (!string.IsNullOrWhiteSpace(filter.Title))
            {

                _where += string.Format(" and Title like '%{0}%'", filter.Title);

            }
            if (filter.Mid.HasValue)
            {
                _where += string.Format(" and Mid = {0}", filter.Mid.Value);
            }

            if (filter.Volume.HasValue)
            {
                _where += string.Format(" and Volume > {0}", filter.Volume.Value);
            }

            if (filter.OrderType != OrdrRuleEnum.Default)
            {


                switch (filter.OrderType)
                {
                    case OrdrRuleEnum.Default:
                        break;
                    case OrdrRuleEnum.PricesLowToHigh:
                        pageInfo.field = "CouponAfterPrice";
                        pageInfo.order = "asc";
                        break;
                    case OrdrRuleEnum.PricesHighToLow:
                        break;
                    case OrdrRuleEnum.VolumeLowToHigh:
                        break;
                    case OrdrRuleEnum.VolumeHighToLow:
                        pageInfo.field = "Volume";
                        pageInfo.order = "desc";
                        break;
                    default:
                        break;
                }
            }

            return GetListByFilter(filter, pageInfo, _where);
        }

        public int OverData()
        {
            var date = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
            var dateNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string _where = string.Format("where CreateTime < '{0}'", date);
            _where += "or (CouponAmount != 0 and CouponEndTime <= '" + dateNow + "' )";
            return MaterialGoodsRepository.DeleteByWhere(_where);
        }

        public dynamic GetDistinctListByFilter(MaterialGoodsModel filter, PageInfo pageInfo)
        {

            string _where = " where 1=1";
            if (filter.S_Price.HasValue)
            {

                _where += string.Format(" and CouponAfterPrice >= {0}", filter.S_Price.Value);
            }
            if (filter.B_Price.HasValue)
            {
                _where += string.Format(" and CouponAfterPrice <= {0}", filter.B_Price.Value);

            }
            if (!string.IsNullOrWhiteSpace(filter.Title))
            {

                _where += string.Format(" and Title like '%{0}%'", filter.Title);

            }
            if (filter.Mid.HasValue)
            {
                _where += string.Format(" and Mid = {0}", filter.Mid.Value);
            }

            if (filter.Volume.HasValue)
            {
                _where += string.Format(" and Volume > {0}", filter.Volume.Value);
            }

            if (filter.OrderType != OrdrRuleEnum.Default)
            {

                switch (filter.OrderType)
                {
                    case OrdrRuleEnum.Default:
                        break;
                    case OrdrRuleEnum.PricesLowToHigh:
                        pageInfo.field = "CouponAfterPrice";
                        pageInfo.order = "asc";
                        break;
                    case OrdrRuleEnum.PricesHighToLow:
                        break;
                    case OrdrRuleEnum.VolumeLowToHigh:
                        break;
                    case OrdrRuleEnum.VolumeHighToLow:
                        pageInfo.field = "Volume";
                        pageInfo.order = "desc";
                        break;
                    default:
                        break;
                }
            }
            int total = 0;
            var list = MaterialGoodsRepository.GetMaterialGoodsDistinct(pageInfo, out total, _where);
            return Pager.Paging(list, total);
        }


        public IEnumerable<MaterialGoodsModel> GetDistinctListByFilter(MaterialGoodsModel filter, PageInfo pageInfo, out int total)
        {

            string _where = " where 1=1";
            if (filter.S_Price.HasValue)
            {

                _where += string.Format(" and CouponAfterPrice >= {0}", filter.S_Price.Value);
            }
            if (filter.B_Price.HasValue)
            {
                _where += string.Format(" and CouponAfterPrice <= {0}", filter.B_Price.Value);

            }
            if (!string.IsNullOrWhiteSpace(filter.Title))
            {

                _where += string.Format(" and Title like '%{0}%'", filter.Title);

            }
            if (filter.Mid.HasValue)
            {
                _where += string.Format(" and Mid = {0}", filter.Mid.Value);
            }

            if (filter.Volume.HasValue)
            {
                _where += string.Format(" and Volume > {0}", filter.Volume.Value);
            }

            //if (filter.OrderType != OrdrRuleEnum.Default)
            //{

            switch (filter.OrderType)
            {
                case OrdrRuleEnum.Default:
                    //默认按照Id排序
                    pageInfo.field = "Id";
                    pageInfo.order = "desc";
                    break;
                case OrdrRuleEnum.PricesLowToHigh:
                    pageInfo.field = "CouponAfterPrice";
                    pageInfo.order = "asc";
                    break;
                case OrdrRuleEnum.PricesHighToLow:
                    break;
                case OrdrRuleEnum.VolumeLowToHigh:
                    break;
                case OrdrRuleEnum.VolumeHighToLow:
                    pageInfo.field = "Volume";
                    pageInfo.order = "desc";
                    break;
                default:
                    //默认按照销量排序
                    //pageInfo.field = "Volume";
                    //pageInfo.order = "desc";
                    break;
            }
            // }

            var list = MaterialGoodsRepository.GetMaterialGoodsDistinct(pageInfo, out total, _where);
            return list;
        }
    }

}